﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentApartMentManage
{
    public partial class AddAMTInfo : Form
    {
        public AddAMTInfo()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void AddAMTInfo_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DataShow.show("select * from 公寓楼房信息表");
            dataGridView2.DataSource = DataShow.show("select * from 公寓信息表");
            comboBox2.ValueMember = "管理员ID";
            comboBox2.DisplayMember = "管理员ID";
            comboBox2.DataSource = sqlHelper.GetTable("select distinct 管理员ID from 公寓管理员信息表");
            comboBox3.ValueMember = "公寓楼号";
            comboBox3.DisplayMember = "公寓楼号";
            comboBox3.DataSource = sqlHelper.GetTable("select distinct 公寓楼号 from 公寓楼房信息表");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {


                string sql = string.Format("insert into 公寓楼房信息表 values('{0}','{1}','{2}',{3},{4},{5},'{6}','{7}')", textBox1.Text.Trim(), textBox2.Text.Trim(), textBox3.Text.Trim(), radioButton1.Checked ? 1 : 0, Convert.ToInt32(textBox4.Text.Trim()), Convert.ToInt32(comboBox1.Text.Trim()), comboBox2.Text.Trim(), dateTimePicker1.Value);
                int j = sqlHelper.Execute(sql);
                if (j != -1)
                {
                    MessageBox.Show("添加楼房信息成功!");
                    dataGridView1.DataSource = DataShow.show("select * from 公寓楼房信息表");
                }
                else
                    MessageBox.Show("添加楼房信息失败!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败!");
                return;
            
            }           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要重置输入的信息吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Text = "0";
                comboBox1.Text = "0";
                radioButton1.Checked = true;
                comboBox2.Text = "";
                dateTimePicker1.Value = System.DateTime.Now;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if(textBox6.Text.Trim()!="0" && checkBox3.Checked==true)
                {
                    MessageBox.Show("不存在寝室住满但仍有床位数量的寝室!");
                    return;
                }
                string sql = string.Format("insert into 公寓信息表 values('{0}','{1}',{2},{3},{4})", textBox5.Text.Trim(), comboBox3.Text.Trim(),Convert.ToInt32(textBox6.Text.Trim()), Convert.ToInt32(textBox7.Text.Trim()), checkBox3.Checked ? 1 : 0);
                int j = sqlHelper.Execute(sql);
                if (j != -1)
                {
                    MessageBox.Show("添加宿舍信息成功!");
                    dataGridView2.DataSource = DataShow.show("select * from 公寓信息表");
                }
                else
                    MessageBox.Show("添加宿舍信息失败!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败!");
                return;

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要重置输入的信息吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                textBox5.Clear();
                textBox6.Text = "5";
                textBox7.Text = "500";
                comboBox3.Text = "";
                checkBox3.Checked = false;
            }
        }
    }
}
